package com.jiantong.web.open.app.work;

import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.service.BreedFactoryReadService;
import com.jiantong.web.core.enums.ExceptionCode;
import com.jiantong.work.model.VeterinaryDrugsReport;
import com.jiantong.work.service.VeterinaryDrugsReportReadService;
import com.jiantong.work.service.VeterinaryDrugsReportWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Strings;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

@Slf4j
@OpenBean
public class VeterinaryDrugsReportApis {

    @RpcConsumer
    private VeterinaryDrugsReportReadService veterinaryDrugsReportReadService;
    @RpcConsumer
    private VeterinaryDrugsReportWriteService veterinaryDrugsReportWriteService;

    @RpcConsumer
    private BreedFactoryReadService breedFactoryReadService;
    /*
        根据ID查询一条数据
     */
    @OpenMethod(
            key = "find.veterinary.report.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public VeterinaryDrugsReport findById(Long id){
        if(id == null){
            log.error("id should be not bull");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not bull");
        }
        Response<VeterinaryDrugsReport> response = veterinaryDrugsReportReadService.finById(id);
        if (!response.isSuccess()){
            log.error("failed to find veterinary report read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    response.getError());
        }
        return response.getResult();
    }

    @OpenMethod(
            key = "create.veterinary.report",
            paramNames = {"veterinaryReport","toStatus"},
            httpMethods = RequestMethod.POST)
    public Map<String,Object> create(VeterinaryDrugsReport veterinaryDrugsReport,Integer toStatus){
        Response<Map<String,Object>> response = veterinaryDrugsReportWriteService.create(veterinaryDrugsReport,toStatus);
        //如果没成功
        if(!response.isSuccess()){
            log.error("failed to create veterinary drugs report veterinaryDrugsReport = {}, cause : {}", veterinaryDrugsReport, response.getError());
            throw new OPServerException(ExceptionCode.DATA_WRITE_FAIL.value(),
                    response.getError());

        }
        return response.getResult();
    }
    @OpenMethod(
            key = "update.veterinary.report",
            paramNames = {"veterinaryReport","toStatus"},
            httpMethods = RequestMethod.PUT)
    public Map<String,Object> update(VeterinaryDrugsReport veterinaryDrugsReport,Integer toStatus){
        if(veterinaryDrugsReport.getId() == null){
            log.error("id should be not null error = {}, cause : {}", veterinaryDrugsReport,veterinaryDrugsReport.getId());
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }

        Response<Map<String , Object>> statusResponse = veterinaryDrugsReportReadService.findStatusById(veterinaryDrugsReport.getId());
        if(statusResponse.isSuccess() && !statusResponse.getResult().get("status").equals(veterinaryDrugsReport.getStatus())){
            log.error("veterinaryDrugsReport is already submitted error = {}, cause : {}", veterinaryDrugsReport.getStatus(),statusResponse.getResult().get("status"));
            throw new OPClientException(ExceptionCode.DOCUMENT_REPEAT_SUBMIT.value(),
                    "this.report.is.already.submitted");
        }
        Response<Map<String,Object>> response = veterinaryDrugsReportWriteService.update(veterinaryDrugsReport,toStatus);
        if(!response.isSuccess()){
            log.error("failed to create veterinary report veterinaryDrugsReport = {} ,case:{}",veterinaryDrugsReport,response.getError());
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(),
                    response.getError());
        }
        return response.getResult();
    }
    @OpenMethod(
            key = "paging.veterinary.report",
            paramNames = {"pageNo", "pageSize","status","breedFactoryId"},
            httpMethods = RequestMethod.GET)
    public Paging<VeterinaryDrugsReport> pagingVeterinaryDrugsReport(Integer pageNo, Integer pageSize,
                                                                     Integer status, Long breedFactoryId){
        if(breedFactoryId == null){
            log.error("breedFactoryId should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "breedFactoryId should be not null");
        }
        Response<Paging<VeterinaryDrugsReport>> resp =
                veterinaryDrugsReportReadService.pagingVeterinaryDrugsReport(pageNo, pageSize, status,breedFactoryId);
        if (!resp.isSuccess()){
            log.error("failed to paging veterinary report by id = {}, cause : {}", status, resp.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    resp.getError());
        }
        return resp.getResult();
    }

    /**
     * 兽药中心 --政府端时间查询
     *
     * @return status =1
     */
    @OpenMethod(
            key = "paging.status.veterinary.report",
            paramNames = {"pageNo", "pageSize", "breedFactoryId","breedType", "fromDate", "toDate"},
            httpMethods = RequestMethod.GET)
    public Paging<VeterinaryDrugsReport> pagingVeterinaryDrugsReports(Integer pageNo,
                                                                      Integer pageSize,
                                                                      Long breedFactoryId,
                                                                      Integer breedType,
                                                                      String fromDate,
                                                                      String toDate) {
        Date veterinaryStart = null;
        if (!Strings.isNullOrEmpty(fromDate)) {
            veterinaryStart = DateTime.parse(fromDate).toDate();
        }

        Date veterinaryEnd = null;
        if (!Strings.isNullOrEmpty(toDate)) {
            veterinaryEnd = DateTime.parse(toDate).toDate();
        }
        Response<Paging<VeterinaryDrugsReport>> resp =
                veterinaryDrugsReportReadService.pagingVeterinaryDrugsReports(pageNo, pageSize, breedFactoryId, breedType, veterinaryStart, veterinaryEnd);
        if (!resp.isSuccess()) {
            log.error("failed to paging veterinary report{}, cause : {}", resp.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    resp.getError());
        }
        return resp.getResult();

    }
}

